package com.zhnc.dangjianpingtaiapp.utils.recycler.grid;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zhnc.dangjianpingtaiapp.R;

/**
 * 下标 布局界面
 */
public class MyPageIndicator extends LinearLayout {
    public MyPageIndicator(Context context) {
        this(context, null);
    }

    public MyPageIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private int itemsNumber;

    /**
     * 设置下标个数
     *
     * @param itemsNumber
     */
    public void InitIndicatorItems(int itemsNumber) {
        this.itemsNumber = itemsNumber;
        removeAllViews();
        for (int i = 0; i < itemsNumber; i++) {
            ImageView imageView = new ImageView(getContext());
            if (i == 0) {
                imageView.setImageResource(R.drawable.dot_selected);
            } else {
                imageView.setImageResource(R.drawable.dot_unselected);
            }
            imageView.setPadding(10, 0, 10, 0);
            addView(imageView);
        }
    }

    public void onPageSelected(int pageIndex) {
        for (int i = 0; i < itemsNumber; i++) {
            onPageUnSelected(i);
        }

        ImageView imageView = (ImageView) getChildAt(pageIndex);
        if (imageView != null) {
            imageView.setImageResource(R.drawable.dot_selected);
        }
    }

    public void onPageUnSelected(int pageIndex) {
        try {
            ImageView imageView = (ImageView) getChildAt(pageIndex);
            if (imageView != null) {
                imageView.setImageResource(R.drawable.dot_unselected);
            }
        } catch (Exception e) {
        }

    }
}
